package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dao.InformationDao;
import com.example.dao.Spend_listDao;
import com.example.domain.Books;
import com.example.domain.Information;
import com.example.domain.Spend_list;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.util.Date;
import java.util.List;

@Service
public class Spend_listService {

    @Autowired
    private Spend_listDao spend_listDao;
    @Autowired
    private InformationDao informationDao;

    /*
    添加充值记录
     */
    public void rechargeRecord(Integer recharge, Long uId){
        QueryWrapper<Information> qw = new QueryWrapper<>();
        qw.eq("uId",uId);
        Information information = informationDao.selectOne(qw);
        String name = information.getName();
        Double balance = information.getBalance();
        Long uId1 = information.getUId();
        Date date = new Date();
        Integer biaoji = 1;
        Spend_list spend_list = new Spend_list();
        spend_list.setUserId(uId1);
        spend_list.setUserName(name);
        spend_list.setRecharge(recharge);
        spend_list.setBalancechange(recharge+0.0);
        spend_list.setBalance(balance);
        spend_list.setTime(date);
        spend_list.setBiaoji(biaoji);
        spend_listDao.insert(spend_list);
    }

    /*
    根据userId查询记录
     */
    public List<Spend_list> selectallById(Long userId){
        QueryWrapper<Spend_list> qw = new QueryWrapper<>();
        qw.eq("userid",userId);
        return spend_listDao.selectList(qw);
    }

    /*
    添加购书记录
     */
    public void consumeBook(Books book, Long userId){
        QueryWrapper<Information> qw = new QueryWrapper<>();
        qw.eq("uId",userId);
        Information information = informationDao.selectOne(qw);
        Double balance = information.getBalance();
        String name = information.getName();
        Spend_list spend_list = new Spend_list();
        spend_list.setBiaoji(0);
        spend_list.setTime(new Date());
        spend_list.setBalance(balance);
        spend_list.setBalancechange(book.getBookPrice());
        spend_list.setConsumeBookName(book.getBookName());
        spend_list.setUserName(name);
        spend_list.setUserId(userId);
        spend_listDao.insert(spend_list);
    }
}
